/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package YottaTron.communication;

public class WatchDog extends Thread {

    CommunicationUDP comm;
    long time;
    boolean run;
    int timeOut;

    /*
     * timeOut je v ms
     */
    public WatchDog(CommunicationUDP comm, int timeOut) {
        this.comm = comm;
        this.timeOut = timeOut;
        run = true;
        this.start();
    }

    public void resetTime() {
        time = System.nanoTime();
    }

    public void end() {
        run = false;
    }

    @Override
    public void run() {
        time = System.nanoTime();
        while (run) {
            try {
                Thread.sleep(50);
            } catch (Exception e) {
            }

            if ((System.nanoTime() - time)/1000000 > timeOut) {
                comm.sendHugeDataRequest();
                time = System.nanoTime();
                System.out.println("WATCHDOG                     haf haf!");
            }
        }
    }
}
